import { defineStore} from 'pinia'
import {ref} from 'vue'

export const useUserStore = defineStore('user', () => {
  const token = ref('')
  const userInfo = ref({})
  const address = ref([{
     name: 'zs',
     phone: '12345678901',
     dz: '江西省赣州市章贡区湖边镇江西环境工程职业学院',
     mo: true
  }])
  const addAdd = (item) => {
    address.value.unshift(item)
  }
  // 存用户信息
  const setUserinfo = (info) => {
    userInfo.value = info
  }
  // 存储token
  const setToken = (to) => {
    token.value = to
  }
  // 退出登录
  const removeToken = () => {
    token.value = ''
    userInfo.value = {}
  }
  
  // 重定向的 Object 对象
  const redirectInfo = ref(null)
  const updateRedirectInfo = (info) => {
    redirectInfo.value = info
    console.log(info);
  }
  
  return {
    address,
    addAdd,
    token,
    setUserinfo,
    setToken,
    removeToken,
    redirectInfo,
    updateRedirectInfo
  }
},
{
    // 配置持久化
    persist: {
      // 调整为兼容多端的API
      storage: {
        setItem(key, value) {
          uni.setStorageSync(key, value) // [!code warning]
        },
        getItem(key) {
          return uni.getStorageSync(key) // [!code warning]
        },
      },
    },
  })